home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / gufw / instance.py < prev    next >
Text File  |  2009-10-25  |  3KB  |  91 lines

  1. # Gufw 9.10.4 - http://gufw.tuxfamily.org
  2. # Copyright (C) 2009 Raul Soriano & Marcos Alvarez Costales
  3. #
  4. # Gufw is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. # Gufw is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with Gufw; if not, see http://www.gnu.org/licenses for more
  14. # information.
  15.  
  16.  
  17. import gtk
  18. import os
  19. import os.path
  20. from model.Variable import Variable
  21.  
  22.  
  23. variable = Variable()
  24.  
  25.     
  26. # Check if gufw is root application
  27. class Check:
  28.  
  29.     def is_root(self):
  30.     
  31.         if os.geteuid() == 0:
  32.             return True
  33.         else:
  34.             dlg = gtk.MessageDialog(None, buttons=gtk.BUTTONS_CLOSE, message_format=variable.get_text("018"))
  35.             dlg.run()
  36.             dlg.destroy()
  37.             return False
  38.         
  39.         
  40. # This class manages application instances
  41. class Instance:
  42.  
  43.     #specify the file where the pid is stores (pid file)
  44.     def __init__(self):
  45.         self.pid_file = variable.get_path("pid_file")
  46.         self.check()
  47.         self.startApplication()
  48.  
  49.     #check wether the app is running
  50.     def check(self):
  51.         #check wether the pid file exists
  52.         if not os.path.isfile(self.pid_file):
  53.             return
  54.  
  55.         #read the pid from file
  56.         pid = 0
  57.         try:
  58.             file = open(self.pid_file, 'rt')
  59.             data = file.read()
  60.             file.close()
  61.             pid = int(data)
  62.         except:
  63.             pass
  64.  
  65.         #check wether the proccess specified exists
  66.         if 0 == pid:
  67.             return
  68.         try:
  69.             os.kill(pid, 0)  #this raises an exception if the pid is invalid
  70.         except:
  71.             return
  72.  
  73.         #exit the program
  74.         exit(0) # exit reaises an exception, so there is no need for a try/except block
  75.  
  76.     #called when there is no running instances, storing the new pid (creating a instance)
  77.     def startApplication(self):
  78.         file = open(self.pid_file, 'wt')
  79.         file.write( str(os.getpid()))
  80.         file.close()
  81.  
  82.     #called when the running insnstance exits, removing the existing pid file (destroying the existing instance)
  83.     def exitApplication(self):
  84.     # Close WindowsxitApplication(self):
  85.         try:
  86.             os.remove(self.pid_file)
  87.         except:
  88.             pass
  89.